package 综合应用.T5;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.*;

@SuppressWarnings("all")
public class T5 {

    public static void main(String[] args) throws Exception {
        List<Dept> depts = init();
        System.out.println("============================员工管理系统======================================");
        once:
        while (true) {
            System.out.println("=====================================================================");
            System.out.println("【1】注册");
            System.out.println("【2】登录");
            System.out.println("【3】退出");
            Scanner sc = new Scanner(System.in);
            String num = sc.nextLine();
            switch (num) {
                case "1":
                    register(depts);
                    break;
                case "2":
                    Boolean flag = login(depts);
                    if (flag) {
                        two:
                        while (true) {
                            System.out.println("=================================员工信息管理==============================");
                            System.out.println("【1】查看员工信息");
                            System.out.println("【2】查询人数最大的部门信息");
                            System.out.println("【3】查询部门信息");
                            System.out.println("【4】退出");
                            Scanner sc1 = new Scanner(System.in);
                            String num1 = sc.nextLine();
                            switch (num1) {
                                case "1":
                                    findAll(depts);
                                    break;
                                case "2":
                                    findMaxDept(depts);
                                    break;
                                case "3":findDept(depts);
                                    break ;
                                default:
                                    break two;
                            }
                        }
                    }
                    break;
                default:
                    break once;
            }
        }
    }


    private static void findDept(List<Dept> depts) {
        System.out.println("=================================查询部门信息==============================");
        System.out.println("部门编号\t\t部门名称\t\t部门人数");
        for (Dept dept : depts) {
            System.out.println(dept.getDeptno()+"\t\t"+dept.getDname()+"\t\t"+dept.getEmpList().size());
        }
    }


    private static void findMaxDept(List<Dept> depts) {
        System.out.println("=================================最大部门信息==============================");
        int maxCount = depts.get(0).getEmpList().size();
        for (Dept dept : depts) {
            if (maxCount<dept.getEmpList().size()){
                maxCount = dept.getEmpList().size();
            }
        }

        for (Dept dept : depts) {
            if (dept.getEmpList().size()==maxCount){
                System.out.println("部门名称["+dept.getDname()+"]，部门人数："+maxCount+"人");
                System.out.println("该部门人员有：");
                StringBuilder sb = new StringBuilder();
                for (Emp emp : dept.getEmpList()) {
                    sb.append(emp.getEname()).append(" ");
                }
                System.out.println(sb.toString() );
            }
        }
    }


    private static void findAll(List<Dept> depts) {

        System.out.println("=================================查询员工信息==============================");
        System.out.println("员工编号\t\t员工姓名\t\t部门名称");
        for (Dept dept : depts) {
            List<Emp> empList = dept.getEmpList();
            for (Emp emp : empList) {
                System.out.println(emp.getNo()+"    \t\t"+emp.getEname()+"\t\t"+dept.getDname());
            }
        }
    }

    private static Boolean login(List<Dept> depts) throws Exception {

        Properties empPros = getEmpPros();

        Scanner sc = new Scanner(System.in);
        System.out.println("请输入用户名：");
        String username = sc.nextLine();
        for (String s : empPros.stringPropertyNames()) {
            if (s.equals(username)){
                String storePwd = empPros.getProperty(s);
                System.out.println("请输入密码：");
                String pwd = sc.nextLine();
                if (storePwd.equals(pwd)){
                    System.out.println("登录成功");
                    return true;
                }else {
                    System.out.println("用户名或密码错误，请重新登录");
                    return false;
                }
            }
        }
        System.out.println("该用户不存在，请注册或者重新登录！");
        return false;
    }


    private static void register(List<Dept> depts) throws Exception {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入用户名：");
        String username = sc.nextLine();
        Dept dept0 = null;
        aa:for (Dept dept : depts) {
            List<Emp> empList = dept.getEmpList();
            for (Emp emp : empList) {
                if (emp.getEname().equals(username)){
                    dept0 = dept;
                    break aa;
                }
            }
        }
        if (dept0==null){
            System.out.println("已经查实"+username+"不是我们公司员工");
            return;
        }
        Properties empPro = getEmpPros();

        for (String s : empPro.stringPropertyNames()) {
            if (s.equals(username)){
                System.out.println("该用户已经被注册，请重新登录");
                return;
            }
        }
        System.out.println("该用户名是 大数据["+dept0.getDname()+"]员工 "+username+" 已查实是我公司员工，可以注册");

        System.out.println("请输入密码：");
        String pwd = sc.nextLine();
        System.out.println("请输入确认密码：");
        String rePwd = sc.nextLine();
        if (pwd.equals(rePwd)){
            empPro.setProperty(username,pwd);
            updateEmpPros(empPro);
            System.out.println("注册成功 ");
        }else {
            System.out.println("两次密码输入不一样，注册失败");
        }
    }


    public static void updateEmpPros(Properties empPros) throws IOException {
        FileOutputStream fos = new FileOutputStream(new File("综合应用/T5/emp.properties"));
        empPros.store(fos,new SimpleDateFormat("yyyy-MM-dd  HH:mm:ss").format(new Date()));
        fos.close();
    }

    public static Properties getEmpPros() throws IOException {
        Properties pro = new Properties();
        FileInputStream fis = new FileInputStream(new File("综合应用/T5/emp.properties"));
        pro.load(fis);
        fis.close();
        return pro;
    }


    private static List<Dept> init() throws Exception {

        List<Dept> deptList = getDeptList0();
        List<Emp> empList = getEmpList0();
        for (Dept dept : deptList) {
            for (Emp emp : empList) {
                if (dept.getDeptno().equals(emp.getDeptno())){
                    dept.getEmpList().add(emp);
                }
            }
        }

        return deptList;
    }

    private static List<Emp> getEmpList0() throws DocumentException {
        List<Emp>list = new ArrayList<>();
        SAXReader sr = new SAXReader();
        Document doc = sr.read("综合应用/T5/emp.xml");
        Element rootElement = doc.getRootElement();
        List<Element> elements = rootElement.elements();
        for (Element element : elements) {
            String no = element.elementText("no");
            String ename = element.elementText("ename");
            String deptno = element.elementText("deptno");
            list.add(new Emp(no,ename,deptno));
        }
        return list;
    }


    private static List<Dept> getDeptList0() throws DocumentException {
        List<Dept>list = new ArrayList<>();
        SAXReader sr = new SAXReader();
        Document doc = sr.read("综合应用/T5/dept.xml");
        Element rootElement = doc.getRootElement();
        List<Element> elements = rootElement.elements();
        for (Element element : elements) {
            String no = element.elementText("no");
            String dname = element.elementText("dname");
            list.add(new Dept(no,dname));
        }
        return list;
    }
}
